package hn.cch.indicator;

import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 峰度
 */
public class Kurtosis implements Indicator {

    public Double indicator(List<Return> R) {

        Double mean = R.stream().collect(Collectors.averagingDouble(Return::getRp));

        double[] Rp = R.stream().map(Return::getRp).mapToDouble(Double::doubleValue).toArray();
        double sigma = new StandardDeviation(false).evaluate(Rp);

        double sum = 0d;
        for (double Rt : Rp) {
            sum += Math.pow((Rt - mean) / sigma, 4);
        }

        int T = R.size();
        double k = 1.0 * T * (T + 1) / ((T - 1) * (T - 2) * (T - 3));
        double b = -3.0 * Math.pow(T - 1, 2) / ((T - 2) * (T - 3));

        Double kurtosis = k * sum - b;
        return kurtosis;
    }

}
